package UI_admin;

import GestionPlannification.Artiste;
import GestionPlannification.Genre;
import GestionPlannification.Piece;
import GestionPlannification.Representation;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Panel permettant de créer ou d'editer une piece
 * @author Elsa Ferrare, Alexandre Becquet
 */
public class Panel_Piece extends javax.swing.JPanel {
    /**
     * Piece du Panel si édiation d'un pièce
     */
    private Piece maPiece=new Piece();
    /**
     * ListModels pour les JList
     */
    private DefaultListModel listModelRepresentations= new DefaultListModel();
    private DefaultListModel listModelArtistes= new DefaultListModel();
    private DefaultListModel listModelAuteurs= new DefaultListModel();
    private DefaultListModel listModelActeurs= new DefaultListModel();
    /**
     * Frame d'administration
     */
    private Frame_administration maframe;

    /**
     * Constructeur d'un panel pour une nouvelle pièce
     * @param frame : la frame d'administration
     */
    public Panel_Piece(Frame_administration frame) {
        this.maframe=frame;
        initComponents();
        //recuperation des genres et ajout dans le combobox
        List<Genre> lesGenres=Genre.findEntities();
        for(int i=0;i<lesGenres.size();i++){            
            this.jComboBoxGenre.addItem(lesGenres.get(i));
        }
        //recuperation des artistes et ajout dans le combobox
        List<Artiste> lesArtistes=Artiste.findEntities();
        for(int i=0;i<lesArtistes.size();i++){
            listModelArtistes.addElement(lesArtistes.get(i));
        }
        this.jListArtistes.setModel(listModelArtistes);
    }

    /**
     * Constructeur d'un Panel pour l'édition d'une pièce déjà existante
     * @param frame : la frame d'administration
     * @param p : la pièce à éditer
     */
    public Panel_Piece(Frame_administration frame,Piece p) {
        this.maframe=frame;
        this.maPiece=p;
        initComponents();

        /*
         * initialisation suivant les attributs de la pièce
         * */
        this.jTextFieldTitre.setText(maPiece.getTitre());
        this.jTextFieldDuree.setText(""+maPiece.getDuree());
        this.jTextAreaResume.setText(maPiece.getResume());
        //affiche de la pièce
        ImageIcon icon = new ImageIcon("image/"+p.getId()+".jpg","Affiche");
        this.jLabelImage.setIcon(icon);
        //auteurs de la pièce
        List<Artiste> lesAuteurs=this.maPiece.getAuteurs();
        for(int i=0;i<lesAuteurs.size();i++){
            this.listModelAuteurs.addElement(lesAuteurs.get(i));
        }
        this.jListAuteurs.setModel(listModelAuteurs);
        //acteurs de la pièce
        List<Artiste> lesActeurs=this.maPiece.getActeurs();
        for(int i=0;i<lesActeurs.size();i++){
            this.listModelActeurs.addElement(lesActeurs.get(i));
        }
        this.jListActeurs.setModel(listModelActeurs);
        //representations de la pièce
        List<Representation> lesRepresentations=this.maPiece.getRepresentations();
        for(int i=0;i<lesRepresentations.size();i++){
            this.listModelRepresentations.addElement(lesRepresentations.get(i));
        }
        this.jListRepresentations.setModel(listModelRepresentations);
        
        
        //récuperations des genres
        List<Genre> lesGenres=Genre.findEntities();
        for(int i=0;i<lesGenres.size();i++){
            this.jComboBoxGenre.addItem(lesGenres.get(i));
            if(maPiece.getGenre().getIdGenre()==lesGenres.get(i).getIdGenre()) this.jComboBoxGenre.setSelectedIndex(i);
        }

        //récuperatons des artistes
        List<Artiste> lesArtistes=Artiste.findEntities();
        for(int i=0;i<lesArtistes.size();i++){
            listModelArtistes.addElement(lesArtistes.get(i));
        }
        this.jListArtistes.setModel(listModelArtistes);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooserAffiche = new javax.swing.JFileChooser();
        jDialogNewArtiste = new javax.swing.JDialog();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jFormattedTextFieldDateNaissance = new javax.swing.JFormattedTextField();
        jTextFieldPrenom = new javax.swing.JTextField();
        jTextFieldNom = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jButtonDialCreer = new javax.swing.JButton();
        jButtonDialAnnuler = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldTitre = new javax.swing.JTextField();
        jTextFieldDuree = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaResume = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jButtonChoisirImage = new javax.swing.JButton();
        jTextFieldAffiche = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxGenre = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListArtistes = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();
        jButtonNouveauArtiste = new javax.swing.JButton();
        jFormattedTextFieldRepresentation = new javax.swing.JFormattedTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        jListRepresentations = new javax.swing.JList();
        jButtonAjouterDate = new javax.swing.JButton();
        jButtonSupprimerDate = new javax.swing.JButton();
        jButtonAjouterActeur = new javax.swing.JButton();
        jButtonAjouterAuteur = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListActeurs = new javax.swing.JList();
        jScrollPane5 = new javax.swing.JScrollPane();
        jListAuteurs = new javax.swing.JList();
        jButtonSuppActeur = new javax.swing.JButton();
        jButtonSuppAuteur = new javax.swing.JButton();
        jButtonEnregistrer = new javax.swing.JButton();
        jButtonAnnuler = new javax.swing.JButton();
        jLabelImage = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jLabel7.setText("Nom :");

        jLabel9.setText("Nouveau artiste");

        jLabel10.setText("Prénom :");

        jFormattedTextFieldDateNaissance.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        jFormattedTextFieldDateNaissance.setText("dd/MM/yyyy");

        jLabel11.setText("Date Naissance :");

        jButtonDialCreer.setText("Créer");
        jButtonDialCreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDialCreerActionPerformed(evt);
            }
        });

        jButtonDialAnnuler.setText("Annuler");
        jButtonDialAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDialAnnulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialogNewArtisteLayout = new javax.swing.GroupLayout(jDialogNewArtiste.getContentPane());
        jDialogNewArtiste.getContentPane().setLayout(jDialogNewArtisteLayout);
        jDialogNewArtisteLayout.setHorizontalGroup(
            jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogNewArtisteLayout.createSequentialGroup()
                .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(jDialogNewArtisteLayout.createSequentialGroup()
                        .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldNom, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                            .addComponent(jTextFieldPrenom)
                            .addComponent(jFormattedTextFieldDateNaissance, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jDialogNewArtisteLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jButtonDialCreer)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonDialAnnuler)))
                .addContainerGap(117, Short.MAX_VALUE))
        );
        jDialogNewArtisteLayout.setVerticalGroup(
            jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogNewArtisteLayout.createSequentialGroup()
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextFieldPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFormattedTextFieldDateNaissance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogNewArtisteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDialCreer)
                    .addComponent(jButtonDialAnnuler))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setText("Titre de la pièce");

        jLabel2.setText("Durée");

        jTextFieldTitre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTitreActionPerformed(evt);
            }
        });

        jLabel3.setText("Resumé");

        jTextAreaResume.setColumns(20);
        jTextAreaResume.setRows(5);
        jScrollPane1.setViewportView(jTextAreaResume);

        jLabel4.setText("Image");

        jButtonChoisirImage.setText("Parcourir");
        jButtonChoisirImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChoisirImageActionPerformed(evt);
            }
        });

        jLabel5.setText("Genre");

        jLabel6.setText("Artistes");

        jScrollPane2.setViewportView(jListArtistes);

        jLabel8.setText("Representation jj/mm/aaaa");

        jButtonNouveauArtiste.setText("Nouveau");
        jButtonNouveauArtiste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNouveauArtisteActionPerformed(evt);
            }
        });

        jFormattedTextFieldRepresentation.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));

        jScrollPane4.setViewportView(jListRepresentations);

        jButtonAjouterDate.setText("Ajouter");
        jButtonAjouterDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterDateActionPerformed(evt);
            }
        });

        jButtonSupprimerDate.setText("Supprimer");
        jButtonSupprimerDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSupprimerDateActionPerformed(evt);
            }
        });

        jButtonAjouterActeur.setText("Ajouter Acteur");
        jButtonAjouterActeur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterActeurActionPerformed(evt);
            }
        });

        jButtonAjouterAuteur.setText("Ajouter Auteur");
        jButtonAjouterAuteur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterAuteurActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(jListActeurs);

        jScrollPane5.setViewportView(jListAuteurs);

        jButtonSuppActeur.setText("Supprimer Acteur");
        jButtonSuppActeur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSuppActeurActionPerformed(evt);
            }
        });

        jButtonSuppAuteur.setText(" Supprimer Auteur");
        jButtonSuppAuteur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSuppAuteurActionPerformed(evt);
            }
        });

        jButtonEnregistrer.setText("Enregistrer");
        jButtonEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnregistrerActionPerformed(evt);
            }
        });

        jButtonAnnuler.setText("Annuler");
        jButtonAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnnulerActionPerformed(evt);
            }
        });

        jButton1.setText("Ajouter Genre");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jButtonNouveauArtiste, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextFieldDuree, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldTitre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                            .addComponent(jTextFieldAffiche, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxGenre, javax.swing.GroupLayout.Alignment.TRAILING, 0, 197, Short.MAX_VALUE)
                            .addComponent(jFormattedTextFieldRepresentation, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonChoisirImage)
                            .addComponent(jButtonSuppAuteur)
                            .addComponent(jButtonAjouterAuteur)
                            .addComponent(jButtonSuppActeur)
                            .addComponent(jButtonAjouterActeur)
                            .addComponent(jButtonAjouterDate)
                            .addComponent(jButtonSupprimerDate)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelImage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonAnnuler)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEnregistrer)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldTitre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldDuree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonChoisirImage)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldAffiche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jComboBoxGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton1)))))
                    .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonNouveauArtiste))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonAjouterActeur)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSuppActeur)
                                .addGap(46, 46, 46)
                                .addComponent(jButtonAjouterAuteur)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSuppAuteur))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addComponent(jButtonAjouterDate)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSupprimerDate))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jFormattedTextFieldRepresentation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAnnuler)
                    .addComponent(jButtonEnregistrer))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldTitreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTitreActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldTitreActionPerformed

    /**
     * Action du Button NouveauArtiste
     * Affichage du dialog pour l'ajout d'un artiste
     * @param evt
     */
    private void jButtonNouveauArtisteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNouveauArtisteActionPerformed
        jDialogNewArtiste.setVisible(true);
}//GEN-LAST:event_jButtonNouveauArtisteActionPerformed

    /**
     * Action du button AjouterDate
     * Ajout d'une date dans les representations de la pièce
     * @param evt
     */
    private void jButtonAjouterDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterDateActionPerformed
        if(this.jFormattedTextFieldRepresentation.getText().length()>0){
            Representation r=new Representation((Date) this.jFormattedTextFieldRepresentation.getValue(),this.maPiece);
            this.listModelRepresentations.addElement(r);
            this.jListRepresentations.setModel(this.listModelRepresentations);
        }
}//GEN-LAST:event_jButtonAjouterDateActionPerformed

    /**
     * Action du button SupprimerDate
     * Suppression d'une date dans les representations de la pièce
     * @param evt
     */
    private void jButtonSupprimerDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSupprimerDateActionPerformed
        if(listModelRepresentations.getSize()>0  && this.jListRepresentations.getSelectedIndex()>=0){
            this.listModelRepresentations.remove(this.jListRepresentations.getSelectedIndex());
            this.jListRepresentations.setModel(this.listModelRepresentations);
        }
}//GEN-LAST:event_jButtonSupprimerDateActionPerformed

    /**
     * Action du button ChoisirImage
     * Affichage du dialog de choix de fichier pour l'ajout d'une image
     * @param evt
     */
    private void jButtonChoisirImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChoisirImageActionPerformed
        int returnVal = jFileChooserAffiche.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.jTextFieldAffiche.setText(jFileChooserAffiche.getSelectedFile().getAbsolutePath());
        }
}//GEN-LAST:event_jButtonChoisirImageActionPerformed

    /**
     * Action du button AjouterActeur
     * Ajout d'un acteur dans la liste de la  pièce
     * @param evt
     */
    private void jButtonAjouterActeurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterActeurActionPerformed
        if(this.jListArtistes.getSelectedIndex()>=0){
            Artiste a=(Artiste) this.listModelArtistes.get(this.jListArtistes.getSelectedIndex());
            this.listModelActeurs.addElement(a);
            this.jListActeurs.setModel(listModelActeurs);
        }
    }//GEN-LAST:event_jButtonAjouterActeurActionPerformed

    /**
     * Action du button AjouterAuteur
     * Ajout d'un auteur dans la liste de la pièce
     * @param evt
     */
    private void jButtonAjouterAuteurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterAuteurActionPerformed
        if(this.jListArtistes.getSelectedIndex()>=0){
            Artiste a=(Artiste) this.listModelArtistes.get(this.jListArtistes.getSelectedIndex());
            this.listModelAuteurs.addElement(a);
            this.jListAuteurs.setModel(listModelAuteurs);
        }
    }//GEN-LAST:event_jButtonAjouterAuteurActionPerformed

    /**
     * Action du button SuppActeur
     * Suppression d'un acteur dans la liste de la pièce
     * @param evt
     */
    private void jButtonSuppActeurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSuppActeurActionPerformed
        if(this.listModelActeurs.getSize()>0 && this.jListActeurs.getSelectedIndex()>=0){
            this.listModelActeurs.remove(this.jListActeurs.getSelectedIndex());
            this.jListActeurs.setModel(this.listModelActeurs);
        }
    }//GEN-LAST:event_jButtonSuppActeurActionPerformed

    /**
     * Action du button SuppAuteur
     * Suppression d'un auteur dans la liste de la pièce
     * @param evt
     */
    private void jButtonSuppAuteurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSuppAuteurActionPerformed
        if(this.listModelAuteurs.getSize()>0 && this.jListAuteurs.getSelectedIndex()>=0){
            this.listModelAuteurs.remove(this.jListAuteurs.getSelectedIndex());
            this.jListAuteurs.setModel(this.listModelAuteurs);
        }
    }//GEN-LAST:event_jButtonSuppAuteurActionPerformed

    /**
     * Action du button Enregistrer
     * Sauvegarde de la pièce (mise à jour ou création)
     * @param evt
     */
    private void jButtonEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEnregistrerActionPerformed
        if(this.listModelAuteurs.getSize()>0 && 
                this.listModelActeurs.getSize()>0 &&
                this.listModelRepresentations.getSize()>0 &&
                this.jTextFieldTitre.getText().length()>0){
            this.maPiece.setTitre(this.jTextFieldTitre.getText());
            this.maPiece.setDuree(Integer.parseInt(this.jTextFieldDuree.getText()));
            this.maPiece.setResume(this.jTextAreaResume.getText());
            this.maPiece.setGenre((Genre)this.jComboBoxGenre.getSelectedItem());
            this.maPiece.removeActeurs();
            for(int i=0;i<this.listModelActeurs.size();i++){
                this.maPiece.addActeur((Artiste) this.listModelActeurs.get(i));
            }
            this.maPiece.removeAuteurs();
            for(int i=0;i<this.listModelAuteurs.size();i++){
                this.maPiece.addAuteur((Artiste) this.listModelAuteurs.get(i));
            }
            this.maPiece.removeRepresentations();
            for(int i=0;i<this.listModelRepresentations.size();i++){
                this.maPiece.addRepresentation((Representation) this.listModelRepresentations.get(i));
            }
            //mise à jour ou création si la pièce a un identifient ou non
            if(this.maPiece.getId()>0){
                Piece.edit(maPiece);
                JOptionPane.showMessageDialog(this, "Pièce mise à jour","",JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                Piece.create(maPiece,this.jTextFieldAffiche.getText());
                JOptionPane.showMessageDialog(this, "Pièce créée","",JOptionPane.INFORMATION_MESSAGE);
            }
            
            
            this.maframe.fermerPanel();
            this.maframe.chargerlesPieces();
        }
        else{
            JOptionPane.showMessageDialog(this, "Erreur sur la saisie","",JOptionPane.ERROR_MESSAGE);
        }
        
}//GEN-LAST:event_jButtonEnregistrerActionPerformed
    /**
     * Action du button Annuler du jDialogNewArtiste
     * Masquer le dialog
     * @param evt
     */
    private void jButtonDialAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDialAnnulerActionPerformed
        this.jDialogNewArtiste.setVisible(false);
}//GEN-LAST:event_jButtonDialAnnulerActionPerformed

    /**
     * Action du button Creer du jDialogNewArtiste
     * Création d'un artiste et ajout dans la liste des artistes
     * @param evt
     */
    private void jButtonDialCreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDialCreerActionPerformed
        Artiste a=new Artiste(this.jTextFieldNom.getText(),this.jTextFieldPrenom.getText(),(Date)this.jFormattedTextFieldDateNaissance.getValue());
        this.listModelArtistes.addElement(a);
        this.jListArtistes.setModel(listModelArtistes);
        this.jDialogNewArtiste.setVisible(false);
        
    }//GEN-LAST:event_jButtonDialCreerActionPerformed

    private void jButtonAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnnulerActionPerformed
        this.maframe.fermerPanel();
    }//GEN-LAST:event_jButtonAnnulerActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonAjouterActeur;
    private javax.swing.JButton jButtonAjouterAuteur;
    private javax.swing.JButton jButtonAjouterDate;
    private javax.swing.JButton jButtonAnnuler;
    private javax.swing.JButton jButtonChoisirImage;
    private javax.swing.JButton jButtonDialAnnuler;
    private javax.swing.JButton jButtonDialCreer;
    private javax.swing.JButton jButtonEnregistrer;
    private javax.swing.JButton jButtonNouveauArtiste;
    private javax.swing.JButton jButtonSuppActeur;
    private javax.swing.JButton jButtonSuppAuteur;
    private javax.swing.JButton jButtonSupprimerDate;
    private javax.swing.JComboBox jComboBoxGenre;
    private javax.swing.JDialog jDialogNewArtiste;
    private javax.swing.JFileChooser jFileChooserAffiche;
    private javax.swing.JFormattedTextField jFormattedTextFieldDateNaissance;
    private javax.swing.JFormattedTextField jFormattedTextFieldRepresentation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JList jListActeurs;
    private javax.swing.JList jListArtistes;
    private javax.swing.JList jListAuteurs;
    private javax.swing.JList jListRepresentations;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTextArea jTextAreaResume;
    private javax.swing.JTextField jTextFieldAffiche;
    private javax.swing.JTextField jTextFieldDuree;
    private javax.swing.JTextField jTextFieldNom;
    private javax.swing.JTextField jTextFieldPrenom;
    private javax.swing.JTextField jTextFieldTitre;
    // End of variables declaration//GEN-END:variables

}
